<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body></body>
  <script>
    //将以下的输入iputArr 转化成 output的形式，数组转为树。

    let inputArr = [
      {
        id: 2,
        val: '班级1',
        parentId: 1,
      },
      {
        id: 4,
        val: '班级3',
        parentId: 1,
      },
      {
        id: 5,
        val: '学生1',
        parentId: 2,
      },
      {
        id: 1,
        val: '学校',
      },
      {
        id: 6,
        val: '学生2',
        parentId: 3,
      },
      {
        id: 3,
        val: '班级2',
        parentId: 1,
      },
      {
        id: 7,
        val: '学生3',
        parentId: 4,
      },
    ];
    function arrToTree(arr, newArr = []) {}
    const tree = arrToTree(inputArr);
    console.log(tree);

    let output = {
      id: 1,
      val: '学校',
      children: [
        {
          id: 2,
          val: '班级1',
          children: [
            {
              id: 5,
              val: '学生1',
            },
          ],
        },
        {
          id: 3,
          val: '班级2',
          children: [
            {
              id: 6,
              val: '学生2',
            },
          ],
        },
        {
          id: 4,
          val: '班级3',
          children: [
            {
              id: 7,
              val: '学生3',
            },
          ],
        },
      ],
    };
  </script>
</html>
